"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = void 0;
const index_1 = __importDefault(require("./cloudUserApi/index"));
const index_2 = __importDefault(require("./signActivity/index"));
const timeCompare_1 = __importDefault(require("./utils/timeCompare"));
const moment_1 = __importDefault(require("moment"));
const wx_server_sdk_1 = __importDefault(require("wx-server-sdk"));
wx_server_sdk_1.default.init({
    env: 'sleep-sign-0g9xr6lm95e07431',
});
function main() {
    return __awaiter(this, void 0, void 0, function* () {
        const wxContext = wx_server_sdk_1.default.getWXContext();
        const log = wx_server_sdk_1.default.logger();
        // 获取所有用户信息
        const { data: configs } = yield index_1.default.getConfigAll();
        const curDate = (0, moment_1.default)();
        const curDay = curDate.day() + '';
        // const curTime = curDate.toLocaleTimeString().slice(0, 5); // 08:08:08 得到时间08:08 云端 是 8:08 PM 所以不能这样
        let curTime = '' +
            (curDate.hour() < 10 ? '0' + curDate.hour() : +curDate.hour()) + ':' +
            (curDate.minute() < 10 ? '0' + curDate.minute() : +curDate.minute());
        configs.forEach((config) => __awaiter(this, void 0, void 0, function* () {
            var _a;
            log.info({
                title: '遍历数据开始',
                name: config.name,
                // data: config,
            });
            if (!config.corn_trigger)
                return;
            if (!config.cookie)
                return;
            if (!config.name)
                return;
            log.info({
                title: '开始判断是否签到',
            });
            (_a = config.corn_trigger) === null || _a === void 0 ? void 0 : _a.forEach((corn_t) => {
                var _a, _b, _c, _e;
                log.warn({
                    corn_t: corn_t.day,
                    curDay: curDay,
                    timelt: timeCompare_1.default.lt(curTime, corn_t.endTime),
                    timegt: timeCompare_1.default.gt(curTime, corn_t.startTime),
                    time: curTime,
                    s_time: corn_t.startTime,
                    e_time: corn_t.endTime,
                });
                const dayOk = corn_t.day == curDay;
                const timeOk = timeCompare_1.default.lt(curTime, corn_t.endTime) &&
                    timeCompare_1.default.gt(curTime, corn_t.startTime);
                if (!timeOk || !dayOk) {
                    return;
                }
                log.info({
                    title: '开始查询签到活动',
                });
                console.log('开始查询签到活动');
                index_2.default
                    .aPromise(corn_t.course, (_a = config.cookie) === null || _a === void 0 ? void 0 : _a.uf, (_b = config.cookie) === null || _b === void 0 ? void 0 : _b._d, (_c = config.cookie) === null || _c === void 0 ? void 0 : _c._uid, (_e = config.cookie) === null || _e === void 0 ? void 0 : _e.vc3)
                    .then((res) => {
                    log.log({
                        activity: res,
                        title: '开始签到',
                    });
                    if (typeof res == 'string')
                        return;
                    console.log('开始签到');
                    index_2.default
                        .activityAll(config.cookie, config.name, res, {})
                        .then((res) => {
                        log.info({
                            title: '签到成功'
                        });
                    });
                });
            });
        }));
        return {
            openid: wxContext.OPENID,
            appid: wxContext.APPID,
            unionid: wxContext.UNIONID,
        };
    });
}
exports.main = main;
main();
